<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="项目ID" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="展项ID" prop="exhibitionId">
        <el-input
          v-model="queryParams.exhibitionId"
          placeholder="请输入展项ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目类别" prop="projectCategory">
        <el-select
          v-model="queryParams.projectCategory"
          placeholder="请选择项目类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SH_DEMAND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="当前阶段" prop="currentStage">
        <el-input
          v-model="queryParams.currentStage"
          placeholder="请输入当前阶段"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="当前状态" prop="currentStatus">
        <el-select
          v-model="queryParams.currentStatus"
          placeholder="请选择当前状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数虎商务" prop="shangwuBiz">
        <el-input
          v-model="queryParams.shangwuBiz"
          placeholder="请输入数虎商务"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['shuhu:project-ledger:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['shuhu:project-ledger:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="展项ID" align="center" prop="exhibitionId" />
      <el-table-column label="项目类别" align="center" prop="projectCategory">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SH_DEMAND_TYPE" :value="scope.row.projectCategory" />
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center" prop="projectCode" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column
        label="启动时间"
        align="center"
        prop="startTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="开工日期"
        align="center"
        prop="startWorkDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="交付时间"
        align="center"
        prop="deliveryDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="收款时间"
        align="center"
        prop="paymentDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="当前阶段" align="center" prop="currentStage" />
      <el-table-column label="当前状态" align="center" prop="currentStatus" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="数虎商务" align="center" prop="shangwuBiz" />
      <el-table-column label="数虎PM" align="center" prop="shangwuPm" />
      <el-table-column label="客户PM" align="center" prop="customerPm" />
      <el-table-column label="客户采购" align="center" prop="customerPurchase" />
      <el-table-column label="需求预算" align="center" prop="demandBudget" />
      <el-table-column label="PO号" align="center" prop="poNumber" />
      <el-table-column label="开工令/PO/合同金额" align="center" prop="contractAmount" />
      <el-table-column label="物品开票-13%" align="center" prop="itemInvoicePercent" />
      <el-table-column label="服务开票-6%" align="center" prop="serviceInvoicePercent" />
      <el-table-column label="已收金额" align="center" prop="receivedAmount" />
      <el-table-column label="未收金额" align="center" prop="outstandingAmount" />
      <el-table-column label="商务流程进展" align="center" prop="bizProcessProgress" />
      <el-table-column label="项目执行进展" align="center" prop="executionProgress" />
      <el-table-column label="交付里程碑" align="center" prop="deliveryMilestone" />
      <el-table-column label="项目交付件" align="center" prop="deliveryItems" />
      <el-table-column label="项目交付地" align="center" prop="deliveryLocation" />
      <el-table-column label="项目备份" align="center" prop="backupInfo" />
      <el-table-column label="合同额是否有新增" align="center" prop="additionalContract" />
      <el-table-column label="是否有新增采购，未发起采购审批" align="center" prop="pendingPurchase" />
      <el-table-column label="税前价" align="center" prop="purchaseTaxFreePrice" />
      <el-table-column label="税率" align="center" prop="purchaseTaxRate" />
      <el-table-column label="是否还有未报销费用（差旅等）" align="center" prop="unreimbursedExpenses" />
      <el-table-column label="未付尾款是否有无需支付的" align="center" prop="unpaidBalanceNoPaymentRequired" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="文件id" align="center" prop="fileIds" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['shuhu:project-ledger:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['shuhu:project-ledger:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProjectLedgerForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProjectLedgerApi, ProjectLedgerVO } from '@/api/shuhu/projectledger'
import ProjectLedgerForm from './ProjectLedgerForm.vue'

/** 项目台账管理 列表 */
defineOptions({ name: 'ProjectLedger' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProjectLedgerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectId: undefined,
  exhibitionId: undefined,
  projectCategory: undefined,
  projectCode: undefined,
  projectName: undefined,
  currentStage: undefined,
  currentStatus: undefined,
  customerName: undefined,
  shangwuBiz: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectLedgerApi.getProjectLedgerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectLedgerApi.deleteProjectLedger(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProjectLedgerApi.exportProjectLedger(queryParams)
    download.excel(data, '项目台账管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>